<?php namespace App\Http\Controllers;

use App\Forum;
use App\ForumDiscuss;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\Http\Services\wxh5pay\config\wxh5pay;
use App\Http\Services\wxh5pay\Order;
use App\Page;
use App\Plan;
use App\PlanJoin;
use App\Profession;
use App\ShareInfo;
use App\User;
use App\UserInfo;
use App\VisitLog;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Jenssegers\Agent\Agent;
use IP2City;

class HomeController extends Controller
{
    public function __construct(Request $request)
    {
        $distArr = IP2City::ip2addr($request->ip());

        $distModel = new VisitLog();
        $distModel->ip = $request->ip();
        $distModel->country  = $distArr[0];
        $distModel->province = $distArr[1];
        $distModel->city     = $distArr[2];
        $distModel->save();
    }

    public function index(Guard $guard){
        return view('pc-page');

//        $agent = new Agent();
//        if(!$agent->isMobile()){
//            return view('pc-page');
//        }
//
//        if($guard->guest()){
//            $user_agent = $_SERVER['HTTP_USER_AGENT'];
////            var_dump($user_agent);die;
//            if (strpos($user_agent, 'MicroMessenger') == true) {
//                return redirect()->route('Api.Wechat.wechatLogin');
//            }
//        }
//
//        $planId = Input::get("plan_id");
//
//        $thatPlan = Plan::whereId($planId)->first();
//
//        if(!$thatPlan){
//            /** @var Plan $thatPlan */
//            $thatPlan = Plan::whereStatus(1)->first();
//        }
//
//
//
//        $allNum = 0;
//        $allAmount = 0;
//        if($thatPlan){
//            //增加虚拟人数
////            if($thatPlan->up_num > 0){
////                $updateTime = strtotime($thatPlan->updated_at);
////                if(time() > ($updateTime + 900)){
////                    $thatPlan->invent_num = (int)$thatPlan->invent_num + (int)$thatPlan->up_num;
////                    $thatPlan->save();
////                }
////            }
//
//            $userJoinNum = PlanJoin::wherePlanId($thatPlan->id)->whereIsPay(1)->count();
//            $allNum = (int)$thatPlan->invent_num + (int)$userJoinNum;
//
//            $allAmount = (float)$thatPlan->amount * (int)$allNum;
//
//            /** @var User $user */
//            $user = $guard->user();
//            if($user){
//                $thatPlanJoin = PlanJoin::wherePlanId($thatPlan->id)->whereUserId($user->id)->whereIsPay(1)->first();
////                if($thatPlanJoin && $thatPlanJoin->status == 3){
////                    return redirect()->route("Home.Page.joinMember");
////                }
////                if($thatPlanJoin && is_null($user->userinfo->job)){
////                    return redirect()->route("Home.Page.joinMember");
////                }
//                if($thatPlanJoin){
//                    $thatPlan->is_join = 1;
//                }else{
//                    $thatPlan->is_join = 0;
//                }
//            }else{
//                $thatPlan->is_join = 0;
//            }
//
//            $outTime = strtotime($thatPlan->end_time) - time();
//
//            if($outTime > 0){
//                $thatPlan->end_time = date("Y/m/d H:i:s",strtotime($thatPlan->end_time));
//            }else{
//                $thatPlan->end_time = 0;
//            }
//
//            $thatPlan->video_url = FileManager::getVideoFromOssUrl($thatPlan->video_id);
//            $thatPlan->banner_url = FileManager::getImgFromOssUrl($thatPlan->banner_img_id);
//
//            if($thatPlan->plan_num > 0)
//                $thatPlan->jindu = round( ((int)$thatPlan->all_num +$thatPlan->invent_num) / (int)$thatPlan->plan_num,2) * 100;
//            else
//                $thatPlan->jindu = 0;
//        }
//
//        $share = ShareInfo::wherePage("home")->first();
//
//        return view('new-index2',['plan'=>$thatPlan,'all_num'=>$allNum,'all_amount'=>$allAmount,"share"=>$share,"user"=>$user]);
    }

    public function indexTwo(Guard $guard){
//        $agent = new Agent();
//        if(!$agent->isMobile()){
//            return view('pc-page');
//        }

//        if($guard->guest()){
//            $user_agent = $_SERVER['HTTP_USER_AGENT'];
////            var_dump($user_agent);die;
//            if (strpos($user_agent, 'MicroMessenger') == true) {
//                return redirect()->route('Api.Wechat.wechatLogin');
//            }
//        }

        $planId = Input::get("plan_id");

        $thatPlan = Plan::whereId($planId)->first();

        if(!$thatPlan){
            /** @var Plan $thatPlan */
            $thatPlan = Plan::whereStatus(1)->first();
        }



        $allNum = 0;
        $allAmount = 0;
        if($thatPlan){
            //增加虚拟人数
//            if($thatPlan->up_num > 0){
//                $updateTime = strtotime($thatPlan->updated_at);
//                if(time() > ($updateTime + 900)){
//                    $thatPlan->invent_num = (int)$thatPlan->invent_num + (int)$thatPlan->up_num;
//                    $thatPlan->save();
//                }
//            }

            $userJoinNum = PlanJoin::wherePlanId($thatPlan->id)->whereIsPay(1)->count();
            $allNum = (int)$thatPlan->invent_num + (int)$userJoinNum;

            $allAmount = (float)$thatPlan->amount * (int)$allNum;

            /** @var User $user */
            $user = $guard->user();
            if($user){
                $thatPlanJoin = PlanJoin::wherePlanId($thatPlan->id)->whereUserId($user->id)->whereIsPay(1)->first();
//                if($thatPlanJoin && $thatPlanJoin->status == 3){
//                    return redirect()->route("Home.Page.joinMember");
//                }
//                if($thatPlanJoin && is_null($user->userinfo->job)){
//                    return redirect()->route("Home.Page.joinMember");
//                }
                if($thatPlanJoin){
                    $thatPlan->is_join = 1;
                }else{
                    $thatPlan->is_join = 0;
                }
            }else{
                $thatPlan->is_join = 0;
            }

            $outTime = strtotime($thatPlan->end_time) - time();

            if($outTime > 0){
                $thatPlan->end_time = date("Y/m/d H:i:s",strtotime($thatPlan->end_time));
            }else{
                $thatPlan->end_time = 0;
            }

            $thatPlan->video_url = FileManager::getVideoFromOssUrl($thatPlan->video_id);
            $thatPlan->banner_url = FileManager::getImgFromOssUrl($thatPlan->banner_img_id);

            $thatPlan->jindu = round((float)$thatPlan->all_num / (float)$thatPlan->plan_num,2) * 100;
        }

        $share = ShareInfo::wherePage("home")->first();

        return view('new-index2',['plan'=>$thatPlan,'all_num'=>$allNum,'all_amount'=>$allAmount,"share"=>$share,"user"=>$user]);
    }

    public function login(Guard $guard){
        if($guard->user()){
            /** @var User $user */
            $user = $guard->user();
            /** @var Plan $thatPlan */
            $thatPlan = Plan::whereStatus(1)->first();
            /** @var PlanJoin $thatPlanJoin */
            $thatPlanJoin = PlanJoin::wherePlanId($thatPlan->id)->whereUserId($user->id)->whereIsPay(1)->whereStatus(3)->first();
            if($thatPlanJoin && (time() < (strtotime($thatPlanJoin->pay_time) + 10))) {
                return redirect()->route('Home.Page.paySuccess');
            }else{
                if($user->phone != "" && !is_null($user->phone)){
                    if(!is_null($user->userinfo->job)){
                        return redirect()->route('Home.Page.index');
                    }else{
                        return redirect()->route('Home.Page.joinMember');
                    }
                }
            }
        }

        return view('register');
    }

    public function loginOut(Guard $guard){

        $guard->logout();
        return view('register');
    }

    public function joinMember(Guard $guard){
        if($guard->guest()){
            return redirect()->route('Home.Page.login');
        }

        /** @var User $user */
        $user = $guard->user();

        if(is_null($user->phone)){
            return redirect()->route('Home.Page.login');
        }

        /** @var Plan $thatPlan */
        $thatPlan = Plan::whereStatus(1)->first();

        $thatPlanJoin = PlanJoin::wherePlanId($thatPlan->id)->whereUserId($user->id)->whereIsPay(1)->first();
//        if($thatPlanJoin) {
//            return redirect()->route('Home.Page.paySuccess');
//        }

        if(!is_null($user->userinfo->job)){
            return redirect()->route('Home.Page.index');
        }

        $allProfession = Profession::wherePid(0)->get();

        return view('join-member',['professions'=>$allProfession]);
    }

    public function userCenter(Guard $guard){
        if($guard->guest()){
            return redirect()->route('Home.Page.login');
        }

        /** @var User $user */
        $user = $guard->user();

        $userInfo = UserInfo::whereUserId($user->id)->first();

        return view('user-center',['userinfo'=>$userInfo]);
    }

    public function page($tag,Request $request){

        /** @var Page $thatPage */
        $thatPage = Page::whereTag($tag)->whereStatus(1)->first();


        if($thatPage && !is_null($thatPage->link)){
            return redirect()->to($thatPage->link);
        }else if(!$thatPage){
            return redirect()->to('/');
        }

        return view('page',['info'=>$thatPage]);
    }

    public function forum(){
        return view('forum');
    }

    public function changeUserInfo(Guard $guard){
        if($guard->guest()){
            return redirect()->route('Home.Page.login');
        }

        /** @var User $user */
        $user = $guard->user();

        $userInfo = UserInfo::whereUserId($user->id)->first();


        return view('change-userinfo',['userinfo'=>$userInfo]);
    }

    public function paySuccess(){
        return view('pay-success');
    }

    public function ipTest(Request $request){
        var_dump(IP2City::ip2addr($request->ip()));
    }

    public function forumIndex(){
        return view('forum.index');
    }

    public function forumHome(Request $request,Guard $guard){
        if($guard->guest()){
            return redirect()->route('Home.Page.login');
        }

        $type = !is_null($request->get("type")) ? $request->get("type") : "hot";

        $forums = null;
        if($type == "hot"){
            $forums = Forum::orderBy("read_num","desc")->orderBy("id","desc")->get();
        }elseif ($type == "new"){
            $forums = Forum::orderBy("id","desc")->get();
        }else{
            $forums = Forum::orderBy("is_essence","desc")->orderBy("id","desc")->get();
        }

        if(!is_null($forums) && count($forums) > 0){
            foreach ($forums as $forum){
                $forum->img_url = FileManager::getImgFromOssUrl($forum->img_id);

                $userInfo = UserInfo::whereUserId($forum->user_id)->first();
                if($userInfo){
                    $userInfo->photo_url = $userInfo ? UserManager::getUserPhotoUrl($userInfo->photo_id) : null;
                }

                $forum->user_info = $userInfo ? $userInfo : null;

                $forumDiscussCount = ForumDiscuss::whereFId($forum->id)->count();
                $forum->discuss_count = $forumDiscussCount;
            }
        }

        return view('forum.home',["forums"=>$forums,"type"=>$type]);
    }

    public function forumDetail(Request $request,Guard $guard){
        if($guard->guest()){
            return redirect()->route('Home.Page.login');
        }

        $id = $request->get("id");
        $thatForum = Forum::whereId($id)->first();
        if(!$thatForum){
            return redirect()->route('Home.Page.index');
        }

        $thatForum->read_num=$thatForum->read_num+1;
        $thatForum->save();


        $thatForum->img_url = FileManager::getImgFromOssUrl($thatForum->img_id);

        $userInfo = UserInfo::whereUserId($thatForum->user_id)->first();
        if($userInfo){
            $userInfo->photo_url = $userInfo ? UserManager::getUserPhotoUrl($userInfo->photo_id) : null;
        }

        $thatForum->user_info = $userInfo ? $userInfo : null;

        $allForumDiscuss = ForumDiscuss::whereFId($thatForum->id)->orderBy("id","desc")->get();
        if(count($allForumDiscuss) > 0){
            foreach ($allForumDiscuss as $forumDiscus){


                $fromUserInfo = UserInfo::whereUserId($forumDiscus->user_id)->first();


               /* if($fromUserInfo)$fromUserInfo->photo_url = UserManager::getUserPhotoUrl($fromUserInfo->photo_id);
                $forumDiscus->from_user_info = $fromUserInfo ? $fromUserInfo : null;*/

                if(!$fromUserInfo){

                    $forumDiscus->photo_url = FileManager::getImgFromOssUrl($forumDiscus->photo_id);
                    $forumDiscus->from_user_nickname=$forumDiscus->nick_name;

                }

                else{
                    $forumDiscus->photo_url = UserManager::getUserPhotoUrl($fromUserInfo->photo_id);
                    $forumDiscus->from_user_nickname = $fromUserInfo ->nickname;

                }



                $toUserInfo = UserInfo::whereUserId($forumDiscus->to_user_id)->first();
                if($toUserInfo)$toUserInfo->photo_url = UserManager::getUserPhotoUrl($toUserInfo->photo_id);
                $forumDiscus->to_user_info = $toUserInfo ? $toUserInfo : null;
            }
        }

        return view('forum.detail',["forum"=>$thatForum,"allForumDiscuss"=>$allForumDiscuss]);
    }
}
